using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(SpriteRenderer))]
[RequireComponent(typeof(Collider2D))]
public class UpgradePickup:ItemPickup {
	override protected void Start() {
		base.Start();
		spriteRenderer.sprite=upgrade.upgradeData.sprite;
	}
	private void SetUpgrade(UpgradeInstance upgrade) {
		this.upgrade=upgrade;
		if(spriteRenderer!=null) spriteRenderer.sprite=UpgradeManager.upgradeData[upgrade.upgradeData.id].sprite;
	}

	static GameObject pickablePrefab;
	public static UpgradePickup Create(Vector2 position,UpgradeInstance upgrade) {
		if(pickablePrefab==null) pickablePrefab=Resources.Load<GameObject>("UpgradePickup");
		GameObject gameObject = Instantiate(pickablePrefab,position,Quaternion.identity,Utility.dynamicParent.transform);
		UpgradePickup pickup = gameObject.GetComponentInChildren<UpgradePickup>();
		pickup.SetUpgrade(upgrade);
		return pickup;
	}
	public static UpgradePickup Create(Vector2 position,int id) {
		UpgradeInstance upgrade = new UpgradeInstance(UpgradeManager.upgradeData[id]);
		
		return (Create(position,upgrade));
	}
	public static UpgradePickup Create(Vector2 position,UpgradeData upgrade) {
		UpgradeInstance _upgrade = new UpgradeInstance(upgrade);
		return (Create(position,_upgrade));
	}

	public UpgradeInstance upgrade { get; private set; }
	protected override void HandleInteract() {
		if(!CostPrice(Player.instance)) return;
		if(Player.instance.inventory.GiveItem(new Inventory.ItemUpgrade(upgrade))) Consume();
	}

	public override string GetDescr() { return upgrade.description; }
	public override string GetName() { return upgrade.name; }
}